package com.apexsoft.assets.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.ILiveServiceStub;
import com.apexsoft.LiveServiceException;
import com.apexsoft.assets.base.response.AResponse;
import com.apexsoft.assets.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RefreshScope//spring的注解，表示当前的属性，需要动态刷新
@RestController
public class NacosControl {


//    @LiveClient( namespace =  "${ams.server.namespace}", springCloudApplication = "assets-phoenix-base")
//    private ZgTestService remoteService;

    @Value("${ams.server.namespace:}")
    private String nameSpace;

    @Autowired
    private ILiveServiceStub iLiveServiceStub;


    @Value("${nacos.test.property:123}")
    private String testProperties;


    @Autowired
    private TestService testService;

    @RequestMapping("/sql/test")
    public String sqltest() throws Exception {
        return testService.queryYWSQBH();
    }

    // nacos  配置测试
    @RequestMapping("/nacosconfig/test")
    public String test(){
        return testProperties;
    }


    // liveservice 通用客户端测试
    @RequestMapping("/test/4/1")
    public AResponse test41() throws LiveServiceException {
//        Map map = new HashMap<>();
//        map.put("name", "41");

//        前端请求结构json
        JSONObject object = new JSONObject();
        object.put("name", "41");

        Map mapType = JSON.parseObject(object.toJSONString(), Map.class);

        try {
            return iLiveServiceStub.exec(
                    "com.apexsoft.assets.liveservice.ZgTestService",
                    "sayHello",
                    mapType,       //  gson转换有缺陷   String输入41输出41.0
                    AResponse.class,
                    nameSpace,
                    "");
        } catch (LiveServiceException e) {
            log.error("", e);
            throw e;
        }
    }

//    // liveservice 通用客户端测试
//    @RequestMapping("/test/2/1")
//    public AResponse test21() {
//        Map map = new HashMap<>();
//        map.put("name", nameSpace);
//        return remoteService.sayHello(map);
//    }
}
